/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.sys>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: main.cpp
 *
 * Purpose: app entry
 *
 * Developer:
 *   wen.gu , 2020-07-29
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include "type_parser.h"
#include "utils.h"
#include "key_word.h"

#include "log.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/

int main(int argc, char*argv[])
{

    if (argc < 2)
    {
        return 0;
    }
    LOGD("[DBG][%s.%d]:===>:\n", __FUNCTION__, __LINE__);

    YAML::Node root;
    try {
        root = YAML::LoadFile(argv[1]);
    }catch(const YAML::ParserException& e)
    {
        LOGE("parse yaml file failed: %s\n", e.what());
        return 0;
    }
    catch (const YAML::BadFile& e)
    {
         LOGE("invliad sidl file: %s\n", e.what());
        return 0;       
    }
    
    LOGD("[DBG][%s.%d]:===>:\n", __FUNCTION__, __LINE__);
    if (!root.IsSequence())
    {
        LOGD("[DBG][%s.%d]:===>:\n", __FUNCTION__, __LINE__);
        if (!HasMember(root, KW_SERVICE))
        {
            LOGE("[ERR]:havn't got service keyword\n");            
            return 0;
        }
        LOGD("[DBG][%s.%d]:===>:\n", __FUNCTION__, __LINE__);

        YAML::Node service = root[KW_SERVICE];

        if (!HasMember(service, KW_TYPE_DEF))
        {
            LOGE("[ERR]:havn't got typedef keyword\n");            
            return 0;            
        }
#if 0
        LOGD("[DBG][%s.%d]:===>:\n", __FUNCTION__, __LINE__);
        TypeParser tp;
        YAML::Node type_def = service[KW_TYPE_DEF];
        bool ret = tp.parse(type_def);
        LOGD("[DBG][%s.%d]:===>:\n", __FUNCTION__, __LINE__);
        if (!ret)
        {
            LOGE("[ERR]:parse typedef block failed\n");
            return 0;
        }

        LOGD("[DBG][%s.%d]:===>:\n", __FUNCTION__, __LINE__);
        std::string file_str = tp.dump();
        std::string header_str = tp.dumpHeaderFile();
        LOGD("[DBG]:the type defs:\n%s\n%s\n", header_str.c_str(), file_str.c_str());
#endif        

    }
    else
    {
        LOGE("[ERR]: it is a invailid sidl file\n");
    }



    return 0;
}





